package com.atguigu.lease.web.admin.interceptors;

import com.atguigu.lease.common.excetion.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import java.rmi.dgc.Lease;
import java.sql.SQLInvalidAuthorizationSpecException;

/**
 * @author 芦博
 * @description 用于拦截登录请求
 * @createDate 2025/11/7 18:55
 */
public class LoginInterceptor implements HandlerInterceptor {

    //在请求到达 Controller 的处理方法之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token_value = request.getHeader("Access_token");
        if (ObjectUtils.isEmpty(token_value)){
            //如果不携带请求头就是未登录,判断登录的依据就是请求头
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        JwtUtils.parseToken(token_value);
        return true;
    }
}
